<?php

declare(strict_types=1);

namespace App\Model;

use App\Helpers\AliyunUtil;
use Hyperf\DbConnection\Model\Model;

/**
 *  礼物
 */
class Gift extends Model
{
    protected $table = 'gift';

    //系统礼物
    const TYPE_SYS = 2;
    //普通礼物
    const TYPE_NORMAL = 1;
    //背包礼物
    const TYPE_PACKBAG = 3;
    //金币礼物
    const TYPE_COIN = 4;
    //注册女生礼物
    const TYPE_FEMALE_REGISTER = 5;

    /**
     * 礼物信息
     */
    static function getInfo($giftId = 1)
    {
        $data = self::find($giftId);
        return $data ? $data->toArray() : [];
    }

    /**
     * 批量礼物信息
     */
    static function getBatchInfo($giftIds = [])
    {
        $list = self::whereIn('id', $giftIds)->orderBy('order_index', 'asc')->get()->toArray();
        $data = [];
        foreach ($list as $item) $data[$item['id']] = $item;
        return $data;
    }

    /**
     * 系统礼物
     */
    static function sysGift($key = '')
    {
        $data = self::where('type', self::TYPE_SYS)->first();
        if (!$data) return null;
        return !$key ? $data->toArray() : $data[$key];
    }

    /**
     * 系统礼物ID
     */
    static function getSysGiftIds()
    {
        $where = [
            'type' => self::TYPE_SYS,
            'flag' => 1,
        ];
        return self::where($where)->pluck('id')->toArray();
    }

    /**
     * 获取普通礼物列表
     */
    static function getList($type = self::TYPE_NORMAL)
    {
        $where = [
            'type' => $type,
            'flag' => 1,
        ];
        return self::where($where)->orderBy('order_index', 'asc')->get()->toArray();
    }

    /**
     * 构建列表元素
     */
    static function buildItem($giftItem = [])
    {
        return [
            'id' => $giftItem['id'],
            'name' => $giftItem['name'],
            'image_host' => AliyunUtil::IMG_HOST_V2,
            'icon' => $giftItem['icon'], //AliyunUtil::imageResize($giftItem['icon'], 120, 120),
            'show_image' => $giftItem['show_image'],
            'coin' => $giftItem['coin'],
        ];
    }

    /**
     * 是否背包礼物
     */
    static function checkPackBagGift($giftItem = [])
    {
        return $giftItem && $giftItem['type'] == self::TYPE_PACKBAG;
    }
}
